<?php
if (strpos($_SERVER['PHP_SELF'],basename(__FILE__)) !== false) {
  print "Direct module access forbidden.";exit;}

	global $dbi;
	//$sql "SELECT inpid, issueid, gid, ttask, esttk, idusr, mtime,cost"
	$gid  = $_GET['gid'];
	$iid  = $_GET['issueid']; 
	$sql  = "SELECT DISTINCT mtime,esttk FROM z_issues_cost ";
	$sql .= "WHERE issueid= '$iid' AND gid = '$gid' ";
	$sql .= "AND ( esttk = '5' OR esttk = '1' ) ";
	$sql .= "ORDER BY mtime  ";
	
	$t_mtim = $dbi->fetch_all($sql,'array');
	if(is_array($t_mtim))
	{
		// Init Field Cat
		$o_cfcat = new CFieldCat(array('utime','idusr'));
		
		// Issues 
		$o_issue = new Issue($iid);
		$s_iis = $o_issue->get_detail();
		// Get Task Type
		$t_task = get_task($s_iis['ttype']);
		if(is_array($t_task))
		{
			foreach($t_task as $key => $value)
			{
				$o_cfcat->Add('icost',$value);
				$o_cfcat->setFieldAttr($value,"SetDescr",$value);
			}
		}
		
		//Date Loop
		$t_line = array();
		foreach($t_mtim as $cle => $s_mtim)
		{
			$t_line[$cle]['utime']   = $s_mtim['mtime'];
			$mtime   = $s_mtim['mtime'];
			$esttk   = $s_mtim['esttk'];
			
			$sql  = "SELECT DISTINCT idusr FROM z_issues_cost ";
			$sql .= "WHERE issueid= $iid AND gid = $gid AND mtime ='$mtime' AND esttk = '$esttk' ";
			$t_line[$cle]['idusr']  = $dbi->fetch_one($sql);
			
			$sql  = "SELECT DISTINCT ttask,cost FROM z_issues_cost ";
			$sql .= "WHERE issueid= $iid AND gid = $gid AND mtime ='$mtime' AND esttk = '$esttk' ";
			$t_cost = $dbi->fetch_all($sql,'array');
			foreach($t_task as $key => $value)
				$t_line[$cle][$value] = 0.0;
			
			foreach($t_cost as $id => $s_cost)
			{
				$task = $s_cost['ttask'];
				$cost = $s_cost['cost'];
				$t_line[$cle][$task] = $cost/8.0;
				$t_task[$task] = $task;
			}
		}
		
		$o_grid = new Grid();
		$o_grid->setFieldCat($o_cfcat);
		$o_grid->setFieldAttr('utime',"DisplayTime");
		$o_grid->setDatas($t_line);	
		$o_popup = new XPopup('PopGrid',$lg->chup ,$o_grid->Display(),155,10,480);	
		echo $o_popup->Display();		
	}
?>